﻿/**
*	Class: User
*
*	This class will be used to represent an user account. It will manage the projects and server.
*
*	@param username - user's username
*	@param password - user's password
*/
function User(nextId) {

	this._project = 0;
	this._nextId = nextId;
	//this.server = new Server("http://widgetcastor.appspot.com");
	
	this.projects = [];

}

// Defining getters
User.prototype.getUsername = function() {
	return this._username;
};

/**
*	Returns the current focused project.
*
*	@return Project - current focused project
*/
User.prototype.getProject = function() {
	return this.projects[this._project];
	
};

/**
*	Generates an id which won't match any already existent.
*
*	@return int - unique id
*/
User.prototype.nextId = function(){
	this._nextId += 1;
	return this._nextId;
};

/**
*	Says if there is a project to the right side.
*
*	@return boolean
*/
User.prototype.hasNextProject = function() {
	return (this.projects[this._project+1]!=null);
};

/**
*	Moves focus on the project of the right side.
*/
User.prototype.nextProject = function() {
	if (this.hasNextProject()) {
		this._project++;
	}
};

/**
*	Says if there is a project to the left side.
*
*	@return boolean
*/
User.prototype.hasPrevProject = function() {
	return (this.projects[this._project-1]!=null);
};

/**
*	Moves focus on the project of the right side.
*/
User.prototype.prevProject = function() {
	if (this.hasPrevProject()) {
		this._project--;
	}
};

/**
*	Adds an empty project.
*/
User.prototype.createProject = function(){
	var today = new Date();
	var newProject = new Project(DEFAULT_PROJECT_NAME, today, today);
	this.projects.push(newProject);
	this._project = this.projects.length - 1;
};

/**
*	Removes the project in focus.
*/
User.prototype.removeProject = function(){
	if(this.projects.length <= 1){
		var today = new Date();
		this.projects[0] = new Project(DEFAULT_PROJECT_NAME, today, today);
	} else {
		var i;
		for(i = this._project; i < (this.projects.length - 1); i++){
			this.projects[i] = this.projects[i + 1];
		}
		this.projects.pop();
		if(this._project >= this.projects.length){
			this._project = this.projects.length - 1;
		}
	}
};


User.prototype.setDefaultData = function(){
	this.projects.push(new Project(DEFAULT_PROJECT_NAME, new Date(),new Date()));
};


/**
*	Extract post-it into a XML format to communicate with the app. server.
*
*	@return String - the post-it in xml document format
*/
User.prototype.genXml = function(){
	var xml = new String("");
	xml += "<"  + TAG_USER + ">";
	xml += tag(TAG_NEXT_ID, this._nextId);
	for(var i = 0; i < this.projects.length; i++){	
		xml += this.projects[i].genXml();
	}
	xml += "</"  + TAG_USER + ">";
	return xml;
};
